
#include "aky_program/aky_com_include.h"
static lv_obj_t *bar_bat_large;          //
static lv_obj_t *label_turn_on;          //
static lv_obj_t *img_bat_large;          //
static lv_obj_t *img_bat_flash_large;          //
static lv_obj_t *label_bat_percent_large;          //
static void my_event_cb(lv_obj_t * obj, lv_event_t event);

uint32_t u32_tick_last = 0;
uint32_t u32_tick_last_2 = 0;

uint8_t flag_syn_disp_charge = 0;

uint8_t flag_syn_power_on = 0;
uint8_t syn_send_cnt = 0;

/*
 * 界面回调函数
 */
static void screen013_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    static uint8_t flag_flash_disp = 0;
    static uint16_t time_cnt = 0;


    off_time_clr();
    screen_flush_pro();
    if (g_flag_low_bat == 1)
    {
        if (g_u32_tick_cnt - u32_tick_last > 10000 / 10)
        {
            u32_tick_last = g_u32_tick_cnt;
            info_power_off();
        }
        if (g_u32_tick_cnt - u32_tick_last_2 > 1000 / 10)
        {
            u32_tick_last_2 = g_u32_tick_cnt;
            if (flag_flash_disp == 1)
            {
                flag_flash_disp = 0;
                lv_obj_set_hidden(img_bat_large, true);
            }
            else
            {
                flag_flash_disp = 1;
                lv_obj_set_hidden(img_bat_large, false);
            }
        }

        if (get_cur_charge_stat() != NO_CHARGE)
        {
            g_flag_low_bat = 0;
            lv_obj_set_hidden(bar_bat_large, false);
            lv_obj_set_hidden(img_bat_large, false);
            lv_obj_set_hidden(label_turn_on, false);
            lv_obj_set_hidden(label_bat_percent_large, false);
            //低电压触发 充电重新从1%开始
            g_s_bat_flush.bat_percent_disp = 1;
            lv_bar_set_value(bar_bat_large, g_s_bat_flush.bat_percent_disp, LV_ANIM_OFF);
            lv_label_set_text_fmt(label_bat_percent_large, "%d%%", g_s_bat_flush.bat_percent_disp);

        }
        return;
    }
    
    if (flag_syn_power_on == 1)
    {
        if (g_u32_tick_cnt - u32_tick_last > 100 / 10)
        {
            u32_tick_last = g_u32_tick_cnt;
            aky_task_0_send_cmd(TASK0_CHARGE_POWER_ON);
            syn_send_cnt++;
            if (syn_send_cnt > 3)
            {
                syn_send_cnt = 0;
                flag_syn_power_on = 0;
                synwit_ui_load_screen(SCREEN001);
            }
        }
    }
    
    if (flag_syn_disp_charge == 1)
    {
        if (get_bat_flush_init_ready() == 1)
        {
    
            flag_syn_disp_charge = 0;
            lv_obj_set_hidden(bar_bat_large, false);
            lv_obj_set_hidden(img_bat_large, false);
            lv_obj_set_hidden(label_turn_on, false);

            lv_obj_set_hidden(label_bat_percent_large, false);
            lv_bar_set_value(bar_bat_large, g_s_bat_flush.bat_percent_disp, LV_ANIM_OFF);
            printf("flag_syn_disp_charge = 0 bat_percent_disp = %d\r\n",g_s_bat_flush.bat_percent_disp);
            lv_label_set_text_fmt(label_bat_percent_large, "%d%%", g_s_bat_flush.bat_percent_disp);
            lv_obj_set_hidden(img_bat_flash_large, g_s_bat_flush.flag_bat_flash_disp);

        }
    }
    else if (flag_syn_disp_charge == 0)
    {
        if (get_cur_charge_stat() != NO_CHARGE)
        {
            lv_bar_set_value(bar_bat_large, g_s_bat_flush.bat_percent_disp, LV_ANIM_OFF);
            lv_label_set_text_fmt(label_bat_percent_large, "%d%%", g_s_bat_flush.bat_percent_disp);
            lv_obj_set_hidden(img_bat_flash_large, g_s_bat_flush.flag_bat_flash_disp);
        }
        else
        {
            info_power_off();
        }
    
    }
}

static void screen013_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */

    u32_tick_last = g_u32_tick_cnt;
    u32_tick_last_2 = g_u32_tick_cnt;
    flag_syn_disp_charge = 0;
    flag_syn_power_on = 0;
    syn_send_cnt = 0;
    
    bar_bat_large = synwit_ui_find_lv_obj(WIDGET_BAR6);
    label_turn_on = synwit_ui_find_lv_obj(WIDGET_LABEL53);
    img_bat_large = synwit_ui_find_lv_obj(WIDGET_IMAGE19);
    label_bat_percent_large = synwit_ui_find_lv_obj(WIDGET_LABEL28);
    img_bat_flash_large = synwit_ui_find_lv_obj(WIDGET_IMAGE32);
    lv_obj_set_click(label_turn_on, true);
    if (g_flag_low_bat == 1)
    {
        voice_play_single(VIOCE_LOW_BAT);
        lv_obj_set_hidden(bar_bat_large, true);
        lv_obj_set_hidden(label_turn_on, true);
        lv_obj_set_hidden(label_bat_percent_large, true);
        lv_obj_set_hidden(img_bat_flash_large, true);
    }
    else
    {
        if (get_bat_flush_init_ready() == 0)
        {
            flag_syn_disp_charge = 1;
            lv_obj_set_hidden(bar_bat_large, true);
            lv_obj_set_hidden(label_bat_percent_large, true);
            lv_obj_set_hidden(img_bat_flash_large, true);
            lv_obj_set_hidden(img_bat_large, true);
            printf("flag_syn_disp_charge = 1 bat_percent_disp = %d\r\n",g_s_bat_flush.bat_percent_disp);
    
        }
        else
        {
            lv_bar_set_value(bar_bat_large, g_s_bat_flush.bat_percent_disp, LV_ANIM_OFF);
            lv_label_set_text_fmt(label_bat_percent_large, "%d%%", g_s_bat_flush.bat_percent_disp);
            lv_obj_set_hidden(img_bat_flash_large, g_s_bat_flush.flag_bat_flash_disp);
        }
    }
    lv_obj_set_event_cb(label_turn_on, my_event_cb);   /*Assign an event callback*/

    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();
    stat_bar_stop_flush(2);
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen013_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();
    stat_bar_restart_flush();
}

static int screen013_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen013_cb_obj = {
    .favorite_stop_policy = screen013_favorite_stop_policy,
    .on_screen_start = screen013_start,
    .on_screen_stop = screen013_stop,
    .on_screen_timer = screen013_on_timer,
};

static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if (obj == label_turn_on && g_flag_low_bat == 0)
    {
        if (event == LV_EVENT_CLICKED)
        {
            flag_syn_power_on = 1;
        }
    }
}
